package com.ycb.bookserver.controller.admin;

import com.book.result.Result;
import com.ycb.book.dto.CategoryDTO;
import com.ycb.book.entity.Book;
import com.ycb.book.entity.Category;
import com.ycb.bookserver.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/list")
    @ApiOperation("获取全部分类")
    public Result list(){
        log.info("查询所有分类");
        List<Category> categoryList=categoryService.list();
        return Result.success(categoryList);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询分类信息")
    public Result<Category> getById(@PathVariable Long id){
        log.info("查询书籍信息:{}",id);
        Category category=categoryService.getById(id);
        return Result.success(category);
    }

    @PostMapping("/add")
    @ApiOperation("新增分类")
    public Result addCategory(CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }
    @PutMapping("/update")
    @ApiOperation("修改分类信息")
    public Result update(CategoryDTO categoryDTO){
        log.info("修改分类信息:{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }
}
